%{
    This file is part of StemCellQC, a video bioinformatics software
    toolkit for analysis of phase contrast microscopy videos.
    Copyright 2013-2015 Vincent On. [vincenton001-at-gmail.com]

    StemCellQC is free software: you can redistribute it and/or 
    modify it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    StemCellQC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with StemCellQC.  If not, see <http://www.gnu.org/licenses/>.
%}

function varargout = overlay_videos_gui(varargin)
% OVERLAY_VIDEOS_GUI MATLAB code for overlay_videos_gui.fig
%      OVERLAY_VIDEOS_GUI, by itself, creates a new OVERLAY_VIDEOS_GUI or raises the existing
%      singleton*.
%
%      H = OVERLAY_VIDEOS_GUI returns the handle to a new OVERLAY_VIDEOS_GUI or the handle to
%      the existing singleton*.
%
%      OVERLAY_VIDEOS_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in OVERLAY_VIDEOS_GUI.M with the given input arguments.
%
%      OVERLAY_VIDEOS_GUI('Property','Value',...) creates a new OVERLAY_VIDEOS_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before overlay_videos_gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to overlay_videos_gui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help overlay_videos_gui

% Last Modified by GUIDE v2.5 15-Sep-2014 16:29:47

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @overlay_videos_gui_OpeningFcn, ...
                   'gui_OutputFcn',  @overlay_videos_gui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before overlay_videos_gui is made visible.
function overlay_videos_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to overlay_videos_gui (see VARARGIN)

% Choose default command line output for overlay_videos_gui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes overlay_videos_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = overlay_videos_gui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

%% execute buttons

% --- Executes on button press in load_button.
function load_button_Callback(hObject, eventdata, handles)
% hObject    handle to load_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

folder_name = uigetdir;
pastDir = cd;
cd( folder_name );
path = cd;

% the list of files in the directory
list = ls;

% remove '.' and '..' from list
index = find(list(:,1)=='.');
list(index, : ) = [];

cd( pastDir );
%extract features from video
movies = cell(1);
for j = 1 : size(list,1)
    try
        
        hWaitbar = waitbar( (j-1)/size(list,1), ['Loading Video ' num2str(j)]);
        file = list(j,:);
        readerobj = VideoReader( [ path '\' file ], ...
            'tag', ...
            'myreader1' );
        
        vidFrames = read(readerobj);
        
        numFrames = get(readerobj, 'numberOfFrames');
        
        % Create a MATLAB movie struct from the video frames.
        for k = 1 : numFrames
            mov(k).original = rgb2gray( vidFrames(:,:,:,k));
        end
        
        movies{j} = mov;
        clear mov
        close(hWaitbar);
    end
end

set(handles.video1_menu, 'String', list);
set(handles.video2_menu, 'String', list);
set(handles.video3_menu, 'String', list);
set(handles.video4_menu, 'String', list);
set(handles.video5_menu, 'String', list);

handles.movies = movies;

guidata(hObject, handles);


msgbox( [ 'Finished Loading Videos' ] );



% --- Executes on button press in overlay_button.
function overlay_button_Callback(hObject, eventdata, handles)
% hObject    handle to overlay_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

movies = handles.movies
n_mov = length( movies );
n_frames = length( movies{ 1 });
color = zeros( 5, 1 );
% vid_names = cell( 5, 1 );
for i = 1:5
    color( i ) = eval(['get( handles.color' num2str(i) '_menu ,''Value'' )']);
    list = eval([ 'get(handles.video' num2str( i ) '_menu ,''String'' )']); 
%     vid_names{ i } = eval([ 'list( get( handles.video' num2str( i ) '_menu , ''Value''), : )']);
    vid_num(i) = eval(['get( handles.video' num2str( i ) '_menu , ''Value'')']);
end

oim = cell( 1, n_frames );
for i = 1 : n_frames
    cc_im = cell(1);
    
    hWaitbar = waitbar( i/n_frames , ['Overlay Frames - ' num2str(i)]);
    for j = 1 : 5;
        if color( j ) < 8
            cc_im{j} = im2color_channel( movies{vid_num(j)}(i).original , color( j ));
        end
    end
     
    oim{i} = overlay_im( cc_im );
    close(hWaitbar);
end

handles.oim = oim;

guidata(hObject, handles);

msgbox( [ 'Finished Overlaying Videos' ] );

imshow( oim{1}, [], 'Parent',  handles.axes2 );




% --- Executes on button press in save_button.
function save_button_Callback(hObject, eventdata, handles)
% hObject    handle to save_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
oim = handles.oim;
framerate = str2num( get( handles.framerate_edit, 'string' ));

[file,path] = uiputfile('Untitled.mat','Save Data As');
hWaitbar = waitbar( 0 , ['Saving Video']);
im_var2video([ path '\' file ], framerate, oim );
close(hWaitbar);
msgbox( [ file '.avi finished saving' ] );


function framerate_edit_Callback(hObject, eventdata, handles)
% hObject    handle to framerate_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of framerate_edit as text
%        str2double(get(hObject,'String')) returns contents of framerate_edit as a double


% --- Executes during object creation, after setting all properties.
function framerate_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to framerate_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end










%% parameter menus


% --- Executes on selection change in color1_menu.
function color1_menu_Callback(hObject, eventdata, handles)
% hObject    handle to color1_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns color1_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from color1_menu


% --- Executes during object creation, after setting all properties.
function color1_menu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to color1_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in color2_menu.
function color2_menu_Callback(hObject, eventdata, handles)
% hObject    handle to color2_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns color2_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from color2_menu


% --- Executes during object creation, after setting all properties.
function color2_menu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to color2_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in color3_menu.
function color3_menu_Callback(hObject, eventdata, handles)
% hObject    handle to color3_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns color3_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from color3_menu


% --- Executes during object creation, after setting all properties.
function color3_menu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to color3_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in color4_menu.
function color4_menu_Callback(hObject, eventdata, handles)
% hObject    handle to color4_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns color4_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from color4_menu


% --- Executes during object creation, after setting all properties.
function color4_menu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to color4_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in color5_menu.
function color5_menu_Callback(hObject, eventdata, handles)
% hObject    handle to color5_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns color5_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from color5_menu


% --- Executes during object creation, after setting all properties.
function color5_menu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to color5_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in video1_menu.
function video1_menu_Callback(hObject, eventdata, handles)
% hObject    handle to video1_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns video1_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from video1_menu


% --- Executes during object creation, after setting all properties.
function video1_menu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to video1_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in video2_menu.
function video2_menu_Callback(hObject, eventdata, handles)
% hObject    handle to video2_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns video2_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from video2_menu


% --- Executes during object creation, after setting all properties.
function video2_menu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to video2_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in video3_menu.
function video3_menu_Callback(hObject, eventdata, handles)
% hObject    handle to video3_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns video3_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from video3_menu


% --- Executes during object creation, after setting all properties.
function video3_menu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to video3_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in video4_menu.
function video4_menu_Callback(hObject, eventdata, handles)
% hObject    handle to video4_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns video4_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from video4_menu


% --- Executes during object creation, after setting all properties.
function video4_menu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to video4_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in video5_menu.
function video5_menu_Callback(hObject, eventdata, handles)
% hObject    handle to video5_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns video5_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from video5_menu


% --- Executes during object creation, after setting all properties.
function video5_menu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to video5_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
